<?php
namespace App\Api\Controllers;
use Common\ApiController;
use Common\Common;
use Common\Models\Levels;
use Common\Models\Operator;
use Common\Models\Opercate;
use Common\Models\OrderCommissions;
use Common\Models\User;

class OperatorController extends ApiController
{
	public function initialize()
	{
		parent::initialize();
	}


    public function listAction(){
        $opercate = new Opercate();
        $redis_key="operator_list_".$this->params['user_id'];
        $result=$this->cache->get_cache($redis_key);
        $result=[];
        if(!$result){
            $result['level'] = $opercate->find_first($this->user_info['level_id']);
            $result['ad_menu'] = $opercate->find_menu_ad($this->user_info['level_id'],$this->user_info);
            $this->cache->set_cache($redis_key,$result,30*24*3600);
        }
        $this->ok($result);
    }

    /**
     * 当前用户的升级条件
     */
    public function taskAction(){
        $user = new User();
        $level = new Levels();
        $commisson = new OrderCommissions();
        $myinfo = Common::get_username_phone($this->user_info['id']);
        $myinfo['direct_user']=$user->getDirectFansCount($this->user_info['id'],['is_appuser'=>1]);         //计算我登录的直邀
        $myinfo['commisson_money']=Common::number_randtwo($commisson->sum_money_up($this->user_info['id']));                        //计算我的总结算佣金
        $down_level_id = $myinfo['level_id']+1;
        $down_level = $level->find_by_id($down_level_id);
        if(!$down_level){

        }
        $result_down_level['id']=$down_level['id'];
        $result_down_level['name']=$down_level['name'];
        $result_down_level['direct_users_count']=$down_level['direct_users'];
        $result_down_level['enable_money_count']=$down_level['enable_money'];
        $user_percent = Common::number_randtwo($myinfo['direct_user']/$result_down_level['direct_users_count']);
        $money_percent =  Common::number_randtwo($myinfo['commisson_money']/$result_down_level['enable_money_count']);
        $result_down_level['direct_users_percent']=$user_percent>1 ? '100%' : ($user_percent*100).'%';
        $result_down_level['enable_money_percent']=$money_percent>1 ? '100%' : ($money_percent*100).'%';
        $myinfo['down_level']=$result_down_level;
        $this->ok($myinfo);
    }

}
